package kxfang.com.common.core.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;

import com.gyf.immersionbar.ImmersionBar;

import kxfang.com.common.core.activity.Navigate;
import kxfang.com.common.core.util.InputUtil;
import kxfang.com.common.core.viewModel.BaseViewModel;

/**
 * BaseFragment基类
 */
public abstract class BaseFragment<T extends BaseViewModel, V extends ViewDataBinding> extends Fragment {

    protected boolean isViewPrepare = false;// 视图是否准备好
    protected boolean isVisible = false;// 视图是否可见
    protected boolean isFirstLoad = true;// 是否是第一次加载数据
    protected View rootView;
    protected V dataBinding;
    protected T viewModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        if (rootView == null) {
            if (isUseDataBind()) {
                dataBinding = DataBindingUtil.inflate(inflater, getViewLayout(), container, false);
                rootView = dataBinding.getRoot();
            } else {
                rootView = inflater.inflate(getViewLayout(), container, false);
            }
        }
        init();
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isFirstLoad && !isVisible) {
            isVisible = true;
            lazyLoad();
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isViewPrepare = true;
        lazyLoad();
    }

    /**
     * ui界面用户是否可见
     *
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = isVisibleToUser;
        if (isVisibleToUser) {
            lazyLoad();
        }
    }

    // 懒加载，防止界面卡顿，防止数据重复加载
    private void lazyLoad() {
        if (!isViewPrepare || !isVisible || !isFirstLoad) {
            return;
        }
        // 在这里第一次进行数据加载
        viewModel = getViewModel();
        isFirstLoad = false;
        if (viewModel != null) {
            viewModel.initData();
        }
    }

    /**
     * 获取布局文件
     *
     * @return 返回布局文件id
     */
    protected abstract int getViewLayout();

    protected abstract T getViewModel();

    /**
     * 初始化基本数据,如导航栏、状态栏背景等等
     */
    protected void init() {
    }

    ;

    /**
     * 是否要使用dataBinding
     *
     * @return
     */
    protected boolean isUseDataBind() {
        return true;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isViewPrepare = false;
        isVisible = false;
        isFirstLoad = true;
        InputUtil.forceHideSoftKeyboard(getActivity());
        // 优化fragment
        if (rootView != null) {
            if (rootView.getParent() != null) {
                ((ViewGroup) rootView.getParent()).removeView(rootView);
            }
        }
    }

    /**
     * 有导航栏的修改状态栏颜色
     *
     * @param isDark 状态栏的字体颜色是否是黑色
     */
    protected void modifyStatusBarWithTitleBar(boolean isDark) {
        ImmersionBar.with(this).titleBar(Navigate.getInstance(this).getBar()).statusBarDarkFont(isDark).init();
    }

    /**
     * 无导航栏的修改状态栏颜色
     *
     * @param isDark 状态栏的字体颜色是否是黑色
     */
    protected void modifyStatusBar(boolean isDark) {
        ImmersionBar.with(this).statusBarDarkFont(isDark).init();
    }

    /**
     * 是否显示导航栏下的横线
     *
     * @param isVisible
     */
    protected void showBottomLine(boolean isVisible) {
        Navigate.getInstance(this).getBar().showBottomLine(isVisible);
    }
}
